--[[
	
	******************************
	*********** Lollita **********
	****** Skill Level Helper ****
	*********** v 1.0 ************
	******************************
]]
 
local shiftMenu
local lastLevel
local skillOrder
local isWorking
local levelTime
local spellToLevel
local spellLastLevel
local myPing
 

-- Code -------------------------------------------
 
function OnLoad()
    shiftMenu = scriptConfig("Lollita Skill Level Helper", "LollitaSLHelper" .. myHero.charName)
   
	shiftMenu:addParam("isOn", "Auto LevelUp Skills", SCRIPT_PARAM_ONKEYTOGGLE, false, 78)
	
	shiftMenu:addParam("sep1", "LevelUp Config", SCRIPT_PARAM_INFO, "")
	shiftMenu:addParam("getAllAtLevel", "  Force all Skills at level", SCRIPT_PARAM_SLICE, 4, 3, 4, 0)
	
	shiftMenu:addParam("sep2", "LevelUp Order", SCRIPT_PARAM_INFO, "")
	shiftMenu:addParam("maxQ", "  Max Q", SCRIPT_PARAM_SLICE, 2, 1, 4, 0)
	shiftMenu:addParam("maxW", "  Max W", SCRIPT_PARAM_SLICE, 3, 1, 4, 0)
	shiftMenu:addParam("maxE", "  Max E", SCRIPT_PARAM_SLICE, 4, 1, 4, 0)
	shiftMenu:addParam("maxR", "  Max R", SCRIPT_PARAM_SLICE, 1, 1, 4, 0)
	
	shiftMenu:permaShow("isOn")
	
	lastLevel = 0
	isWorking = false
	shiftMenu.isOn = false
		
    PrintChat("Lollita Skill Level Helper - loaded")
end

function OnTick()
	if not shiftMenu.isOn then return end

	
	if myHero.level ~= lastLevel then				
		if isWorking then
			if myHero:GetSpellData(spellToLevel).level ~= spellLastLevel then
				showLeveledSpell(spellToLevel)
				finishWorking()
			elseif GetTickCount() > levelTime + myPing * 2 then
				startWorking()
			end
		else
			if not checkConfig() then
				disableError()
				return 
			end
			startWorking()
		end
		
	end
end

function startWorking()
	if countTable(skillOrder) == 0 then
		finishWorking()
		return
	end

	spellToLevel = table.remove(skillOrder,1)
	spellLastLevel = myHero:GetSpellData(spellToLevel).level

	levelTime = GetTickCount()
	isWorking = true
	myPing = GetLatency()
	LevelSpell(spellToLevel)
end

function finishWorking()
	isWorking = false
	lastLevel = lastLevel + 1
end

function showLeveledSpell(spell)
	local sufix = ""
	if myHero:GetSpellData(spell).level == 1 then
		sufix = "available"
	else
		sufix = "at level " .. myHero:GetSpellData(spell).level
	end

	local text = "\"" .. myHero:GetSpellData(spell).name .. "\" is now " .. sufix
	PrintChat(text)
end

function checkConfig()
	parseSkillOrder()
	return (countTable(skillOrder) == 4 or myHero.level == shiftMenu.getAllAtLevel) 
end

function parseSkillOrder()
	skillOrder = {}
	
	if myHero.level == shiftMenu.getAllAtLevel then
		local spells = {SPELL_1, SPELL_2, SPELL_3}
		local customSpell = false
		for i = 1, 3 do
			if myHero:GetSpellData(spells[i]).level == 0 then
				customSpell = spells[i]
			end
		end
		if customSpell ~= false then
			skillOrder[1] = customSpell
		end
	else	
		skillOrder[shiftMenu.maxQ] = SPELL_1
		skillOrder[shiftMenu.maxW] = SPELL_2
		skillOrder[shiftMenu.maxE] = SPELL_3
		skillOrder[shiftMenu.maxR] = SPELL_4
	end
end

function disableError()
	PrintChat("Lollita Skills Level Helper Turned off")
	PrintChat("There is an error on your LevelUp Order configuration")
	PrintChat("Please select a different order for each spell")
	shiftMenu.isOn = false
end


function countTable(table)
	local numItems = 0
	for k,v in pairs(table) do
		numItems = numItems + 1
	end
	return numItems
end
